package com.ccl.iot.views;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ccl.iot.R;
import com.ccl.iot.device.DeviceIconProvider;

/**
 * 显示设备全套icon的view
 */
public class ModifyStyleItemView extends FrameLayout implements View.OnClickListener {

	Context mContext;
	private View mLlFirst, mLlSecond, mLlThird;
	private View mLlFour, mLlFive, mLlSix;
	private RadioButton mRb4, mRb5, mRb6;

	private ImageView mIvFirstOne, mIvFirstTwo, mIvFirstThree, mIvFirstFour, mIvSecondOne, mIvSecondTwo, mIvSecondThree, mIvSecondFour, mIvThirdOne, mIvThirdTwo, mIvThirdThree, mIvThirdFour;
	private RadioGroup mRg;

	public ModifyStyleItemView(Context context) {
		super(context);
		mContext = context;
		init();
		initListener();
	}

	private void init() {
		View.inflate(mContext, R.layout.items_icon_selector, this);

		mLlFirst = this.findViewById(R.id.icon_ll_first);
		mLlSecond = this.findViewById(R.id.icon_ll_second);
		mLlThird = this.findViewById(R.id.icon_ll_third);

		// modify
		mLlFour = this.findViewById(R.id.icon_ll_four);
		mLlFive = this.findViewById(R.id.icon_ll_five);
		mLlSix = this.findViewById(R.id.icon_ll_six);
		mLlFour.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRg.check(R.id.radio3);
			}
		});
		mLlFive.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRg.check(R.id.radio4);
			}
		});
		mLlSix.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRg.check(R.id.radio5);
			}
		});
		

		mRb4 = (RadioButton) this.findViewById(R.id.radio3);
		mRb5 = (RadioButton) this.findViewById(R.id.radio4);
		mRb6 = (RadioButton) this.findViewById(R.id.radio5);

		mIvFirstOne = (ImageView) this.findViewById(R.id.icon_iv_first_one);
		mIvFirstTwo = (ImageView) this.findViewById(R.id.icon_iv_first_two);
		mIvFirstThree = (ImageView) this.findViewById(R.id.icon_iv_first_three);
		mIvFirstFour = (ImageView) this.findViewById(R.id.icon_iv_first_four);

		mIvSecondOne = (ImageView) this.findViewById(R.id.icon_iv_second_one);
		mIvSecondTwo = (ImageView) this.findViewById(R.id.icon_iv_second_two);
		mIvSecondThree = (ImageView) this.findViewById(R.id.icon_iv_second_three);
		mIvSecondFour = (ImageView) this.findViewById(R.id.icon_iv_second_four);

		mIvThirdOne = (ImageView) this.findViewById(R.id.icon_iv_third_one);
		mIvThirdTwo = (ImageView) this.findViewById(R.id.icon_iv_third_two);
		mIvThirdThree = (ImageView) this.findViewById(R.id.icon_iv_third_three);
		mIvThirdFour = (ImageView) this.findViewById(R.id.icon_iv_third_four);

		mRg = (RadioGroup) this.findViewById(R.id.pop_modify_style_rg);

	}

	private void initListener() {
		if (mLlFirst != null && mLlSecond != null && mLlThird != null && mRg != null) {
			mLlFirst.setOnClickListener(this);
			mLlSecond.setOnClickListener(this);
			mLlThird.setOnClickListener(this);
		}
	}

	public void showSwitchIcon(int devIconId) {
		mIvFirstOne.setImageResource(R.drawable.icon_spot_a_off);
		mIvFirstTwo.setImageResource(R.drawable.icon_spot_a_on);
		mIvFirstThree.setImageResource(R.drawable.icon_device_off);
		mIvFirstFour.setVisibility(View.GONE);

		mIvSecondOne.setImageResource(R.drawable.icon_chandelier_off);
		mIvSecondTwo.setImageResource(R.drawable.icon_chandelier_on);
		mIvSecondThree.setImageResource(R.drawable.icon_device_off);
		mIvSecondFour.setVisibility(View.GONE);

		mIvThirdOne.setImageResource(R.drawable.icon_sofa_lamp_off);
		mIvThirdTwo.setImageResource(R.drawable.icon_sofa_lamp_on);
		mIvThirdThree.setImageResource(R.drawable.icon_device_off);
		mIvThirdFour.setVisibility(View.GONE);

		// modify
		mLlFour.setVisibility(View.VISIBLE);
		mLlFive.setVisibility(View.VISIBLE);
		mLlSix.setVisibility(View.VISIBLE);
		mRb4.setVisibility(View.VISIBLE);
		mRb5.setVisibility(View.VISIBLE);
		mRb6.setVisibility(View.VISIBLE);

		if (mRg != null) {
			mRg.check(getDefCheck(devIconId));
		}
	}

	public void showDoorIcon(int devIconId) {
		mIvFirstOne.setImageResource(R.drawable.icon_close_curtain_off);
		mIvFirstTwo.setImageResource(R.drawable.icon_stop_curtain_off);
		mIvFirstThree.setImageResource(R.drawable.icon_open_curtain_off);
		mIvFirstFour.setVisibility(View.GONE);

		mIvSecondOne.setImageResource(R.drawable.icon_curtain_b_close);
		mIvSecondTwo.setImageResource(R.drawable.icon_curtain_b_stop);
		mIvSecondThree.setImageResource(R.drawable.icon_curtain_b_open);
		mIvSecondFour.setVisibility(View.GONE);

		mIvThirdOne.setImageResource(R.drawable.icon_curtain_c_close);
		mIvThirdTwo.setImageResource(R.drawable.icon_curtain_c_stop);
		mIvThirdThree.setImageResource(R.drawable.icon_curtain_c_open);
		mIvThirdFour.setVisibility(View.GONE);

		if (mRg != null) {
			mRg.check(getDefCheck(devIconId));
		}
	}

	public void setOnIconItemChangeListener(OnCheckedChangeListener listener) {
		mRg.setOnCheckedChangeListener(listener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.icon_ll_first:
			mRg.check(R.id.radio0);
			break;
		case R.id.icon_ll_second:
			mRg.check(R.id.radio1);
			break;
		case R.id.icon_ll_third:
			mRg.check(R.id.radio2);
			break;
		}
	}

	private int getDefCheck(int checkId) {
		int id = 0;
		if (checkId == DeviceIconProvider.ICON_ID.THREE_ICON) {
			id = R.id.radio2;
		} else if (checkId == DeviceIconProvider.ICON_ID.SECOND_ICON) {
			id = R.id.radio1;
		} else {
			id = R.id.radio0;
		}
		return id;
	}

}
